class code6 {
    //括号生成
    int left,right;
    List<String> ret;
    StringBuffer path;
    public List<String> generateParenthesis(int n) {
        ret = new ArrayList<>();
        path = new StringBuffer();

        dfs(n);
        return ret;
    }
    public void dfs(int n) {
        if(path.length()==n*2) {
            ret.add(path.toString());
            return;
        }

        if(left<n) {
            path.append("(");
            left++;
            dfs(n);
            left--;
            path.deleteCharAt(path.length()-1);
        }

        if(left>right) {
            path.append(")");
            right++;
            dfs(n);
            right--;
            path.deleteCharAt(path.length()-1);
        }
    }
}